

#include "MyGame.h"
#include "MyEngine.h"
#include "MyMessages.h"
#include "MyDefinitions.h"
#include "MyUniversal.h"

MyMessages::MyMessages(MyGame* _parent){ parent = _parent; }
MyMessages::~MyMessages(){ clean_messages();		
#ifdef _DEBUG
	printf("destroyed messages list\n");
#endif
}
//*** methods to work with whole messageSwarm
void MyMessages::init_messages()
{
	//clear the list
	messageSwarm = std::list<messageObject*>();
}
//DEINIT -- clears the whole projectile list
void MyMessages::clean_messages () { messageSwarm.clear(); }
//DRAW -- displays all the projectiles on screen, moves them before
void MyMessages::draw_messages (int _t)
{	
	std::list<messageObject*>::iterator i;	
	//iterate through the whole list
	for (i = messageSwarm.begin();i != messageSwarm.end();)
	{
		messageObject& mo = **i;		 
		//if true is returned, this message can be removed
		if (mo.move(_t)) messageSwarm.erase(i++);
		else ++i;
	}
}
void MyMessages::add_text_message(int _time, int _x, int _y, int _x2, int _y2, char* _text, SDL_Color* _c1, SDL_Color* _c2) 
{
	messageInstance* tempMessage = new messageInstance(_time, _x, _y, _x2, _y2, _text, _c1, _c2, parent);
	tempMessage->messageSheet = TTF_RenderText_Blended (parent->parent->font, _text, *_c1);
	messageSwarm.push_back(tempMessage);
}
//message that scrolls on the right border
void MyMessages::add_border_text_message(int _time, char* tx, SDL_Color* _c1) 
{	
	messageInstance* tempMessage = new messageInstance(_time, 0, 0, 0, SCREEN_H-INFOBAR_H-18, tx, _c1, _c1, parent);
	tempMessage->messageSheet = TTF_RenderText_Blended (parent->parent->font, tx, *_c1);
	tempMessage->pos_x = tempMessage->pos_x2 = (SCREEN_W - tempMessage->messageSheet->w) - 2;
	messageSwarm.push_back(tempMessage);
}
//adds effect - type, coords, time?,
void MyMessages::add_effect_message()
{
}
void MyMessages::death_message()
{
	messageInstance* tempMessage = new messageInstance(100,0,0,0,0,"x",0,0, parent);
	#ifdef _WIN32
	tempMessage->messageSheet = load_image("misc\\gameover.jpg");	
	#else
	tempMessage->messageSheet = load_image("misc/gameover.jpg");		
	#endif
	messageSwarm.push_back(tempMessage);
#ifdef _DEBUG
	printf("message displayed game over\n");
#endif
}
//*** methods to work with messageInstance
bool messageInstance::move(int _t)
{
	//time's up, delete
	if ((time -= _t) < 0) return true;
	else
	{
		SDL_Rect* tmp;
		if (c2 != c1) 
		{
			//adjust the colors, so that when time --> 0, the  color --> c2
			int r = int(c1->r + ((c2->r - c1->r)*get_d_time()));
			int g = int(c1->g + ((c2->g - c1->g)*get_d_time()));
			int b = int(c1->b + ((c2->b - c1->b)*get_d_time()));
			SDL_Color* tempcolor = make_color(r,g,b);
			unload_surface(messageSheet);
			messageSheet = TTF_RenderText_Blended (parent->parent->font, text, *tempcolor);
			delete(tempcolor);
		}
		int y; 
		if (pos_y2 > pos_y) y = int(pos_y + ((pos_y2-pos_y)*get_d_time()));
		else y = int(pos_y - ((pos_y-pos_y2)*get_d_time()));
		SDL_BlitSurface(messageSheet,0,parent->parent->screen,tmp = make_rect(pos_x,y, messageSheet->w, messageSheet->h));
		delete(tmp);
		return false;
	}
}

//*** methods to work with effectInstances
effectInstance::effectInstance(std::string s, int s_x,int s_y, int p_x,int p_y, int _number_of_frames, int time_of_effect, MyGame *_p) : size_w(s_x), size_h(s_y), pos_x(p_x), pos_y(p_y), number_of_frames(_number_of_frames)
{	
	act_frame_time = 0;
	act_frame = 0;
	effectSheet = load_image(s);
	//if file not found
	if (!effectSheet) return; //we dont want to end whole app

	frame_time = time_of_effect / number_of_frames;
	parent = _p;
}
bool effectInstance::move(int _t)
{	
	act_frame_time+=_t;

	if ((act_frame_time) > frame_time) 
	{
		act_frame_time -= frame_time;
		act_frame++;
	}	

	//if we have reached the end of effect sheet
	if (act_frame = number_of_frames) return true;

	SDL_Rect* tmp, *tmp1;
		
	SDL_BlitSurface(effectSheet, tmp = make_rect(act_frame*size_w, 0, size_w, size_h), 
		parent->parent->screen, tmp1 = make_rect(pos_x, pos_y, size_w, size_h));
	delete(tmp);
	delete(tmp1);			
	return false;
}
